<?php
/**
 * @version 1.0
 * @author Etienne Audouin <etienne.audouin@ingesup.com>
 * @author Adrien Marianelli <adrien.marianelli@ingesup.com>
 * @package test
 * @subpackage ciunit
 */
class tests extends CI_Controller {
    /**
    * Scenario de test WebNews
    * {@source}
    */
    public function index() {
        $this->truncate();
        
        $this->load->library('unit_test');
        
        $this->load->model('contact_model', 'contactManager');
        
        $this->createContact_test();
        $this->readContact_test();
        $this->readAllContact_test();
        $this->updateContact_test();
        $this->deleteContact_test();

        $this->load->model('mailinglist_model', 'mailManager');
        
        $this->createMailinglist_test();
        $this->readMailinglist_test();
        $this->readAllMailinglist_test();
        $this->updateMailinglist_test();
        $this->deleteMailinglist_test();

        $this->load->model('newsletter_model', 'newsletterManager');

        $this->createNewsletter_test();
        $this->readNewsletter_test();
        $this->readAllNewsletter_test();
        $this->updateNewsletter_test();
        $this->deleteNewsletter_test();
        
        echo $this->unit->report();
        
        $this->truncate();
    }
    /**
     * Vide la base de donnée
     * {@source}
     */
    public function truncate() {
        $this->db->query("TRUNCATE contact");
        $this->db->query("TRUNCATE mailinglist");
        $this->db->query("TRUNCATE newsletter");
    }
    /**
     * Cree un contact
     * {@source}
     */
    public function createContact_test () {
        $name = 'Name_test';
        $first_name = 'FirstName_test';
        $mail = 'Mail_test';
        $test = $this->contactManager->create($name, $first_name, $mail);
        $this->unit->run($test, true, 'Create contact');
    }
    /**
     * Consulte un contact
     * {@source}
     */
    public function readContact_test() {
        $id = 1;
        $name = 'Name_test';
        $first_name = 'FirstName_test';
        $mail = 'Mail_test';
        
        $test_result = array(
            'id' => $id,
            'name' => $name,
            'first_name' => $first_name,
            'mail' => $mail
        );
        
        $test = $this->contactManager->read($id);
        $test = $test[0];
        $test = get_object_vars($test);
        $this->unit->run($test, $test_result, 'Read contact');
    }
    /**
     * Consulte tous les contacts
     * {@source}
     */
    public function readAllContact_test() {
        $id1 = 1;
        $id2 = 2;
        $name2 = 'Name_test2';
        $first_name2 = 'FirstName_test2';
        $mail2 = 'Mail_test2';
        
        $this->contactManager->create($name2, $first_name2, $mail2);
        
        $test = $this->contactManager->readAll();
        
        $contact1 = $this->contactManager->read($id1);
        $contact2 = $this->contactManager->read($id2);
        $test_result = array($contact1[0], $contact2[0]);
        
        $this->unit->run($test, $test_result, 'Read All contact');
    }
    /**
     * Met a jour un contact
     * {@source}
     */
    public function updateContact_test() {
        $id = 1;
        $name = 'Name_test_updated';
        $first_name = 'FirstName_test_updated';
        $mail = 'Mail_test_updated';
        
        $test_result = array(
            'id' => $id,
            'name' => $name,
            'first_name' => $first_name,
            'mail' => $mail
        );
        
        $this->contactManager->update($first_name, $name, $id, $mail);
        
        $test = $this->contactManager->read($id);
        $test = $test[0];
        $test = get_object_vars($test);
                
        $this->unit->run($test, $test_result, 'Update contact');
    }
    /**
     * Supprime un contact
     * {@source}
     */
    public function deleteContact_test() {
        $id = 1;
        $test = $this->contactManager->delete($id);
        
        $this->unit->run($test, true, 'Delete contact');
    }
    /**
     * Cree une liste de diffusion
     * {@source}
     */
    public function createMailinglist_test() {
        $name = 'Name_test';
        $id_contacts = array('1', '2');
        $test = $this->mailManager->create($name, $id_contacts);
        $this->unit->run($test, true, 'Create mailinglist');
    }
    /**
     * Consulte une liste de diffusion
     * {@source}
     */
    public function readMailinglist_test() {
        $id = 1;
        $name = 'Name_test';
        $id_contacts = '1:2';

        $test_result = array(
            'id' => $id,
            'name' => $name,
            'id_contacts' => $id_contacts,
        );

        $test = $this->mailManager->read($id);
        $test = $test[0];
        $test = get_object_vars($test);
        $this->unit->run($test, $test_result, 'Read mailinglist');
    }
    /**
     * Consulte toutes les liste de diffusion
     * {@source}
     */
    public function readAllMailinglist_test() {
        $id1 = 1;
        $id2 = 2;
        $name2 = 'Name_test2';
        $id_contacts2 = array(1,2);

        $this->mailManager->create($name2, $id_contacts2);

        $test = $this->mailManager->readAll();

        $mailinglist1 = $this->mailManager->read($id1);
        $mailinglist2 = $this->mailManager->read($id2);
        $test_result = array($mailinglist1[0], $mailinglist2[0]);

        $this->unit->run($test, $test_result, 'Read All mailinglists');
    }
    /**
     * Met a jour une liste de diffusion
     * {@source}
     */
    public function updateMailinglist_test() {
        $id = 1;
        $name = 'Name_test_updated';
        $id_contacts = '1:2:3';

        $test_result = array(
            'id' => $id,
            'name' => $name,
            'id_contacts' => $id_contacts
        );

        $this->mailManager->update($id, $id_contacts, $name);

        $test = $this->mailManager->read($id);
        $test = $test[0];
        $test = get_object_vars($test);

        $this->unit->run($test, $test_result, 'Update mailinglist');
    }
    /**
     * Supprime une liste de diffusion
     * {@source}
     */
    public function deleteMailinglist_test() {
        $id = 1;
        $test = $this->mailManager->delete($id);

        $this->unit->run($test, true, 'Delete mailinglist');
    }
    /**
     * Cree une news
     * {@source}
     */
    public function createNewsletter_test() {
        $content = 'Content_test';
        $id_mailinglists = array(1,2);
        $title = 'Title_test';
        $signature = 'Signature_test';

        $test = $this->newsletterManager->create($content, $id_mailinglists, $title, $signature);
        $this->unit->run($test, true, 'Create newsletter');
    }
    /**
     * Consulte une news
     */
    public function readNewsletter_test() {
        $id = 1;
        $content = 'Content_test';
        $id_mailinglists = '1:2';
        $title = 'Title_test';
        $signature = 'Signature_test';

        $test_result = array(
            'id' => $id,
            'content' => $content,
            'id_mailinglists' => $id_mailinglists,
            'title' => $title,
            'signature' => $signature,
        );

        $test = $this->newsletterManager->read($id);
        $test = $test[0];
        $test = get_object_vars($test);
        $this->unit->run($test, $test_result, 'Read newsletter');
    }
    /**
     * Consulte toute les news
     * {@source}
     */
    public function readAllNewsletter_test() {
        $id1 = 1;
        $id2 = 2;
        $content2 = 'Content_test2';
        $id_mailinglists2 = array(2,1);
        $title2 = 'Title_test2';
        $signature2 = 'Signature_test2';

        $this->newsletterManager->create($content2, $id_mailinglists2, $title2, $signature2);

        $test = $this->newsletterManager->readAll();

        $newsletter1 = $this->newsletterManager->read($id1);
        $newsletter2 = $this->newsletterManager->read($id2);
        $test_result = array($newsletter1[0], $newsletter2[0]);
        $this->unit->run($test, $test_result, 'Read All newsletters');
    }

    /**
     * Met a jour une news
     * {@source}
     */
    public function updateNewsletter_test() {
        $id = 1;
        $content = 'Content_test';
        $id_mailinglists = array(2,3);
        $title = 'Title_test';
        $signature = 'Signature_test';
        $id_mailinglists_formated = '2:3';

        $test_result = array(
            'id' => $id,
            'content' => $content,
            'id_mailinglists' => $id_mailinglists_formated,
            'title' => $title,
            'signature' => $signature,
        );

        $this->newsletterManager->update($id_mailinglists, $signature, $content, $title, $id);

        $test = $this->newsletterManager->read($id);
        $test = $test[0];
        $test = get_object_vars($test);

        $this->unit->run($test, $test_result, 'Update newsletter');
    }
    /**
     * Supprime une news
     * {@source}
     */
    public function deleteNewsletter_test() {
        $id = 1;
        $test = $this->newsletterManager->delete($id);

        $this->unit->run($test, true, 'Delete newsletter');
    }
}
?>
